package com.attilax.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.logging.Logger;

import com.attilax.io.pathx;

/**
 * 工具类
 * @author freeteam
 *
 */
public class PropX {
	Properties loginprop;
	public PropX(String f) {
		FileInputStream sins=null;
		String value="";
		try {
			  loginprop = new Properties();
			sins = new FileInputStream(f);
			loginprop.load(sins);
		//	value=loginprop.getProperty(name);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}finally{
			if (sins!=null) {
		        try {
					sins.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			//return value;
		}
	}
	
	public PropX(String f, String enc) {
		FileInputStream sins=null;
		String value="";
		try {
			  loginprop = new Properties();
			//sins = ;
			loginprop.load(new InputStreamReader(new FileInputStream(f), enc));
		//	value=loginprop.getProperty(name);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}finally{
			if (sins!=null) {
		        try {
					sins.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			//return value;
		}
	}

	public String getProperty(String name)
	{
		return loginprop.getProperty(name);
	}
	private int getConfig(String path,String name,String encode) {
		try {
			String f =path;// pathx.webAppPath() + "/noticerCfg.txt";
			Properties props = new Properties();

			props.load(new InputStreamReader(new FileInputStream(f), encode));

			int retime = Integer.parseInt(props.getProperty("retrytimes"));
			return retime;
		} catch (Exception e) {
			return 7;
		}
	}
	public static void main(String[] args) {
		
	}
	//获取配置文件的配置
	public static String getConfig(String path,String name){
		FileInputStream sins=null;
		String value="";
		try {
			Properties loginprop = new Properties();
			sins = new FileInputStream(path);
			loginprop.load(sins);
			value=loginprop.getProperty(name);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if (sins!=null) {
		        try {
					sins.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			return value;
		}
	}

	public void setValue(Map propMap, String string) {
		String[] namses=string.split(",");
		for (String nam : namses) {
			if( this.getProperty(nam)==null)
				System.out.println("---- name is null:"+nam);
			else
			propMap.put(nam, this.getProperty(nam));
		}
		
		
	}

	public Map toMap() {
	Set st=	loginprop.keySet();
	Map m=new HashMap();
	for (Object object : st) {
		m.put(object, loginprop.getProperty(object.toString()));
	}
	
		return m;
	}
}
